/*
package socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;

//服务端
public class Server2 {
    private ServerSocket serverSocket;
    //保存所有客户端的输出流用于广播消息
    private PrintWriter[] allOut = {};

    public Server(){
        try {
            System.out.println("正在启动服务端...");
            serverSocket = new ServerSocket(8088);
            System.out.println("服务端启动完毕");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void start(){
        try {
            //Socket socket = null;//阻塞方法
            while (true) {
                System.out.println("等待客户端连接...");
                Socket socket = serverSocket.accept();//阻塞方法
                System.out.println("一个客户端连接了！");
                //启动一个线程负责处理客户端交互
                ClientHandler handler = new ClientHandler(socket);//一个任务
                Thread t = new Thread(handler);//将任务传进去，启动一个线程
                t.start();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        Server server = new Server();
        server.start();
    }

    */
/** 该线程任务负责与指定客户端进行交互 *//*

    private class ClientHandler implements Runnable{
        private Socket socket;
        private String host;//记录当前对应客户端的IP地址

        public ClientHandler(Socket socket){
            this.socket = socket;
            host = socket.getInetAddress().getHostAddress();//通过socket获取远端计算机IP地址(获取到了客户端)
        }

        public void run(){
            PrintWriter pw = null;
            //通过刚接受的socket,获取输入流来读取该客户端发过来的消息
            try {
                InputStream in = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(in, StandardCharsets.UTF_8);
                BufferedReader br = new BufferedReader(isr);
//              String line = br.readLine();15050691858
//              System.out.println("客户端说："+line);

                //通过socket获取输出流给客户端发送信息
                OutputStream out = socket.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(out,StandardCharsets.UTF_8);
                BufferedWriter bw = new BufferedWriter(osw);
                pw = new PrintWriter(bw,true);

                //将该输出流存入到共享数组allOut中
                //1将allOut数组扩容
                allOut = Arrays.copyOf(allOut,allOut.length+1);
                //2将当前pw存入数组最后一个位置
                allOut[allOut.length-1] = pw;
                sendMessage(host+"上线了,当前在线人数"+allOut.length);

                String line;
                while((line = br.readLine()) != null){
                    System.out.println(host+"说："+line);
                    */
/*//*
/将消息发送给客户端
                    pw.println(host+"说："+line);*//*

                    //将消息发送给所有客户端
                    for (int i=0;i<allOut.length;i++) {
                        allOut[i].println(host + "说：" + line);
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                //处理客户端断开连接后的操作
                //将pw从数组allOut中删除
                for (int i=0;i<allOut.length;i++){
                    if (allOut[i]==pw){
                        allOut[i] = allOut[allOut.length-1];
                        allOut = Arrays.copyOf(allOut,allOut.length-1);
                        break;
                    }
                }
                System.out.println(host+"下线了,当前在线人数："+allOut.length);

                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        private void sendMessage(String message){
            System.out.println(message);
                    */
/*//*
/将消息发送给客户端
                    pw.println(host+"说："+line);*//*

            //将消息发送给所有客户端
            for (int i=0;i<allOut.length;i++) {
                allOut[i].println(message);
            }
        }

    }
}


*/
